<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>定义类和属性</title>
</head>
<body>
    
</body>
<script>
    // 定义类
    //私有属性与私有方法类本身可用
    var Animal=function(name,type){
        //private私有属性
        var _type=type;
        //private私有方法
        var isAnimal=function(){
            console.log("私有方法"+_type);
        }
        isAnimal();
        //protected受保护的属性
        var _age=0;
        //访问器
        Object.defineProperty(this,"age",{
            get: function(){
                console.log("查看年龄...");
                return this._age;
            },
            set: function(val){
                //保护
                if(val>100){
                    this._age=20;
                }else{
                    this._age=val;
                }
            }
        });
        //公有属性和方法
        this.type=type;
        this.sayhello=function(){
            console.log("Hello JSOOP");
        }
    }
    var cat=new Animal("小猫咪","母猫");
    cat.age=100;//set
    console.log(cat.age);//get
    console.log(cat.type);
    cat.sayhello();
    //原型prototype
    console.log(Animal.prototype);
    //给类加属性&&方法
    Animal.prototype.color="彩色";
    Animal.prototype.fight=function(){
        console.log("动物打架了"+this.type);
    }
    console.log(cat.color);
    cat.fight();
    var dog=new Animal("小黄","哈巴狗");
    dog.color="白色";
    console.log(dog.color);
    //静态属性static
    Animal.number=90;
    Animal.count=function(){
        console.log("静态方法动物总数"+Animal.number);
    }
    console.log(Animal.number);
    Animal.count();
    //对象不能用静态属性

</script>
</html>